package com.tms.security.service;

import com.tms.domain.Position;
import com.tms.mapper.develop.UserMapper;
import com.tms.mapper.staff.StaffMapper;
import com.tms.security.entity.UserDetail;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Service;

/**
 * @author ozj
 */
@Service
public class UserDetailServiceImpl implements UserDetailsService {

    @Autowired
    UserMapper userMapper;

    @Autowired
    StaffMapper staffMapper;

    @Override
    public UserDetails loadUserByUsername(String no) throws UsernameNotFoundException {
        UserDetail userDetail = userMapper.selectForUserDetailByNo(no);
        if (userDetail == null) {
            userDetail = staffMapper.selectForUserDetailByNo(no);
            if (userDetail != null) {
                userDetail.setPosition(new Position(7, "ROLE_LOGIN"));
            }
        }
        if (userDetail == null) {
            throw new UsernameNotFoundException("账户不存在!");
        }
        return userDetail;
    }
}
